package com.myapp.widget;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myapp.R;

public class MyProgressDialogBottom extends LinearLayout{
	
	private LinearLayout progressdialog1_bottom_ly;
	private List<Map<String,Object>> list;
	private int messageCount;
	private Context context;
	private int lineWidth,lineHeight;
	private TextView bottom_msg;
	
	public MyProgressDialogBottom(Context context) {
		super(context);
	}
	
	public MyProgressDialogBottom(Context context,AttributeSet set) {
		super(context, set);
		LayoutInflater.from(context).inflate(R.layout.progressdialog1_bottom, this, true);
		progressdialog1_bottom_ly = (LinearLayout)findViewById(R.id.progressdialog1_bottom_ly);
		bottom_msg = (TextView)findViewById(R.id.progressdialog1_bottom_msg);
	}
	
	Handler handler = new Handler();
	Runnable r2 = new Runnable() {
		@Override
		public void run() {
			MyProgressDialogBottom.this.setContent(list.get(scrollflag));
			handler.postDelayed(r, 5000);
		}
	};
	int scrollflag = 0;
	Runnable r = new Runnable() {
		@Override
		public void run() {
				//ly.startAnimation(new AnimationUtils().loadAnimation(context, R.anim.scroll_sign_anim_out));
				animateOut();
				handler.postDelayed(r2, 200);
			if(scrollflag+1<=messageCount-1){
				scrollflag++;
			}else{
				scrollflag=0;
			}
		}
	};
	
	public void setContentAndLineHeight(Context context,List<Map<String,Object>> list,DisplayMetrics metrics){
		this.context = context;
		this.list = list;
		this.lineWidth = metrics.widthPixels;
		messageCount = list.size();
		if(messageCount!=0){
			handler.post(r);
		}
		setLineHeight((int)(metrics.heightPixels*0.1f));
	}
	
	public void setContent(Map<String,Object> map){
		//ly.startAnimation(new AnimationUtils().loadAnimation(context, R.anim.scroll_sign_anim_passin));
		animateIn();
		bottom_msg.setText(String.valueOf(map.get("msg")));
	}
	
	private void setLineHeight(int height){
		lineHeight = height;
		//ScreenManager.setMyLayoutParam(progressdialog1_bottom_ly, width, height);
	}
	
	private void animateOut(){
		Animation out = new TranslateAnimation(0,0,0,-lineHeight);
		out.setDuration(300);
		progressdialog1_bottom_ly.startAnimation(out);
	}
	
	private void animateIn(){
		//Animation out = new TranslateAnimation(lineWidth,0,0,0);
		Animation in = new TranslateAnimation(0,0,lineHeight,0);
		in.setDuration(300);
		progressdialog1_bottom_ly.startAnimation(in);
	}
	
}
